home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
DB_CLIPP
/
0411.ZIP
/
FILL.C
< prev
next >
Wrap
Text File
|
1985-06-01
|
3KB
|
161 lines
/*
* Program to read a text file and fill each text line to n chars.
*/
#include "stdio.h"
#define CR '\15'
#define LF '\12'
#define SPACE '\40'
main(argc,argv)
int argc;
char *argv[];
{
int c,
holdc,
outfile,
infile,
linelenf,
cntr;
if (argc == 1)
{
puts("\n");
puts("Syntax : FILL <infile> <outfile> [<n>]\n\n");
puts("Where : infile is the filespec for the input data\n");
puts(" outfile is the filespec for the output data\n\n");
puts(" n is the length to which lines are\n");
puts(" to be padded\n");
puts("Defaults : n=80\n");
puts("\n");
puts("FILL reads each character from the input file until it\n");
puts("sees a carriage return (Hex 0D). It then checks the\n");
puts("current line length; if it is less than the specified\n");
puts("length, it pads it out with blanks. The CR and the next\n");
puts("character after it from the input file are discarded.\n");
puts("The output line is then terminated with a CR and a line\n");
puts("feed (Hex 0A).\n\n");
exit(0);
puts("\nAuthor : Peter Townsend");
puts("\nDate : 01Jun85");
puts("\nVersion : 01.00");
puts("\n");
exit(0);
}
if (argc == 2)
{
puts("\n");
puts("Missing Output Filename\n");
exit(1);
}
if ((infile = open(argv[1],0)) == ERR)
{
puts("\n");
puts("Cannot open input file ");
puts(argv[1]);
exit(1);
}
if ((outfile = open(argv[2],1)) == ERR)
{
if ((outfile = creat(argv[2])) == ERR)
{
puts("\n");
puts("Cannot open/create output file ");
puts(argv[2]);
exit(1);
}
}
if (argc == 3)
{
linelenf = 80;
}
else
{
linelenf = 0;
cntr = 0;
while (((argv[3][cntr]) != '\0') && (cntr <= 2))
{
if (!isdigit(argv[3][cntr]))
{
puts("\n");
puts("Parameter must be a number\n");
exit(1);
}
linelenf = (linelenf * 10) + atoi(argv[3][cntr]);
++cntr;
}
if (linelenf > 255)
{
puts("\n");
puts("Line length should not be greater than 255\n");
exit(1);
}
}
/*
* convert the file
*/
cntr = 1;
while ((c = fgetc(infile)) != EOF)
{
/*
* check for end of line
*/
if (c == CR)
{
fgetc(infile);
c = SPACE;
/*
* pad line out to desired length
*/
while (cntr <= linelenf)
{
putc(c,outfile);
++cntr;
}
c = CR;
putc(c,outfile);
c = LF;
putc(c,outfile);
cntr = 1;
}
else
{
/*
* check for a given line longer than desired length
*/
if (cntr == linelenf)
{
holdc = c;
c = CR;
putc(c,outfile);
c = LF;
putc(c,outfile);
c = holdc;
putc(c,outfile);
cntr = 1;
}
else
{
putc(c,outfile);
++cntr;
}
}
}
/*
* check that last line input was the correct length
*/
c = SPACE;
while ((cntr < linelenf) && (cntr != 1))
{
putc(c,outfile);
++cntr;
}
if (cntr != 0)
{
c = CR;
putc(c,outfile);
c = LF;
putc(c,outfile);
}
fclose(infile);
fclose(outfile);
exit(0);
}